<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_match_choice {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT mc.*, c.name AS choice_name "
                . "FROM tbl_match_choice_choice mc LEFT JOIN tbl_choice c ON mc.choice_id = c.id";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
    
    public function get_all_by_match_id($match_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT mc.*, c.name AS choice_name "
                . "FROM tbl_match_choice mc LEFT JOIN tbl_choice c ON mc.choice_id = c.id "
                . "WHERE mc.match_id = " . $match_id;
        
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function count_all_by_match_id($match_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT mc.*, c.name AS choice_name "
                . "FROM tbl_match_choice mc LEFT JOIN tbl_choice c ON mc.choice_id = c.id "
                . "WHERE mc.match_id = " . $match_id;
        
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $num = mysqli_num_rows($result);
        $db->close_connect();
        return $num;
    }
    
    public function save($id, $match_id, $choice_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($id == 0) {
            $query = "INSERT INTO tbl_match_choice(match_id, choice_id) VALUES (
                " . $match_id . "," . $choice_id . ")";
        } else {
            $query = "UPDATE tbl_match_choice SET "
                    . "match_id = '" . $match_id . "',"
                    . "choice_id = '" . $choice_id . "'"
                    . "WHERE id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT mc.*, c.name AS choice_name "
                . "FROM tbl_match_choice_choice mc LEFT JOIN tbl_choice c ON mc.choice_id = c.id "
                . "WHERE mc.id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_match_choice WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function delete_by_match_id($match_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_match_choice WHERE match_id = " . $match_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
}
